﻿using AutoMapper;
using B.R.RabbitRoot.Domain;
using B.R.RabbitRoot.ErrorCode;
using B.R.RabbitRoot.Infrastructure.Interface;
using B.R.RabbitRoot.Write.API.Applications.Commands.SourceCommands;
using MediatR;

namespace B.R.RabbitRoot.Write.API.Applications.Commandhandlers.SourceCommandhandelrs
{
    /// <summary>
    /// 溯源命令添加处理
    /// </summary>
    public class SourceCreamCommandHandler : IRequestHandler<SourceCreamCommand, APIResult<int>>
    {
        private readonly IBaserepository<SourceInfo> sourceInfoRedsitoty;
        private readonly IMapper mapper;

        public SourceCreamCommandHandler(IBaserepository<SourceInfo> sourceInfoRedsitoty, IMapper mapper)
        {
            this.sourceInfoRedsitoty = sourceInfoRedsitoty;
            this.mapper = mapper;
        }

        public async Task<APIResult<int>> Handle(SourceCreamCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result=new APIResult<int>();
            result.Code=ResultCode.添加成功;
            result.Message = ResultCode.添加成功.ToString();

            var entity = mapper.Map<SourceInfo>(request);
            entity.SourceCreamPerson = request.SourceCreamPerson;
            entity.SourceCreamTime=DateTime.Now;
            result.Data = await sourceInfoRedsitoty.AddAsync(entity);

            return await Task.FromResult(result);
        }
    }
}
